//
//  ChangeNickNameController.m
//  LianShangBao
//
//  Created by 主事丫环HU on 2020/9/8.
//  Copyright © 2020 主事丫环. All rights reserved.
//

#import "ChangeNickNameController.h"

@interface ChangeNickNameController ()
@property (weak, nonatomic) IBOutlet UITextField *nickNameTextField;

@end

@implementation ChangeNickNameController

- (instancetype)init {
    if (self = [super initWithNibName:nil bundle:nil]) {
        self.modalPresentationStyle = UIModalPresentationCustom;
        self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
    
}
- (IBAction)confirmButtonClick:(UIButton *)sender {
    self.nickNameTextField.text = [self filterSpaces:self.nickNameTextField.text];
    self.nickNameTextField.text = [self disable_emoji:self.nickNameTextField.text];
    
    if ([self.nickNameTextField.text isEqualToString:@""]) {
        ShowToastWithText(@"请输入昵称");
        return;
    }
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"userId"] = USER_ID;
    param[@"nickname"] = self.nickNameTextField.text;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [NetWorkTool POST:updateUser param:param success:^(id dic) {
        ShowToastWithText(@"修改成功");
        if (self.changeSuccessfulClick) {
            self.changeSuccessfulClick(self.nickNameTextField.text);
        }
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } other:^(id dic) {
        ShowToastWithText(@"失败请重试!");
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } fail:^(NSError *error) {
        ShowToastWithText(@"失败请重试!");
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } needUser:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)cancelButtonClick:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
// 过滤空格
- (NSString *)filterSpaces:(NSString *)text{
    text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
    text = [text stringByReplacingOccurrencesOfString:@"\n"withString:@""];
    return text;
}
/**
 *  过滤字符串中的emoji
 */
- (NSString *)disable_emoji:(NSString *)text{
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]"options:NSRegularExpressionCaseInsensitive error:nil];
    NSString *modifiedString = [regex stringByReplacingMatchesInString:text
                                                               options:0
                                                                 range:NSMakeRange(0, [text length])
                                                          withTemplate:@""];
    return modifiedString;
}



@end
